<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="linux">
      Linux服务编写及服务开机启动
     </h1>
     <hr/>
     <p>
      这是我参与2022首次更文挑战的第24天，活动详情查看：
      <a href="https://juejin.cn/post/7052884569032392740">
       2022首次更文挑战
      </a>
     </p>
     <h2 id="_1">
      简介
     </h2>
     <p>
      在某些情况下，需要将自己在服务器上面的一个服务用linux Service之类的命令进行启动，并且能够开机启动，本篇文章中就会介绍如何linux下新建自己的服务
     </p>
     <h2 id="_2">
      一、服务编写
     </h2>
     <h3 id="_3">
      编写服务脚本
     </h3>
     <p>
      首先用编辑器自己写一个脚本文件，模板和大致的内容如下（简单的按照下面的进行操作即可）：比如写 /etc/init.d/test
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="c1">### BEGIN INIT INFO</span>
<span class="c1">#</span>
<span class="c1"># chkconfig:   2345 90 10</span>
<span class="c1"># Provides:  location_server</span>
<span class="c1"># Required-Start:   $local_fs  $remote_fs</span>
<span class="c1"># Required-Stop:    $local_fs  $remote_fs</span>
<span class="c1"># Default-Start:    2 3 4 5</span>
<span class="c1"># Default-Stop:     0 1 6</span>
<span class="c1"># Short-Description:    initscript</span>
<span class="c1"># Description:  This file should be used to construct scripts to be placed in /etc/init.d.</span>
<span class="c1">#</span>
<span class="c1">### END INIT INFO</span>

<span class="c1">## Fill in name of program here.</span>
<span class="nv">PROG</span><span class="o">=</span><span class="s2">"location_server"</span>
<span class="nv">PROG_PATH</span><span class="o">=</span><span class="s2">"/opt/location_server"</span> <span class="c1">## Not need, but sometimes helpful (if $PROG resides in /opt for example).</span>
<span class="nv">PROG_ARGS</span><span class="o">=</span><span class="s2">""</span> 
<span class="nv">PID_PATH</span><span class="o">=</span><span class="s2">"/var/run/"</span>

<span class="c1"># 上面的保持原样就行，需要写的start和stop函数</span>
start<span class="o">()</span> <span class="o">{</span>
    <span class="nb">echo</span> <span class="s2">"current PID: </span><span class="nv">$$</span><span class="s2">"</span> <span class="c1">#这句删除也可以</span>
    bin/kibana  &gt; kibana.log <span class="m">2</span>&gt;<span class="p">&amp;</span><span class="m">1</span> <span class="p">&amp;</span>
    <span class="nb">echo</span> <span class="s2">"</span><span class="nv">$!</span><span class="s2">"</span>
    <span class="nb">echo</span> <span class="s2">"</span><span class="nv">$!</span><span class="s2">"</span> &gt;  /var/run/kibana.pid <span class="c1">#将上一个后台进程写入到标准的pid文件和路径中</span>
<span class="o">}</span>

stop<span class="o">()</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">[</span> -f  <span class="s2">"/var/run/kibana.pid"</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
        <span class="nb">echo</span> <span class="s2">"Will kill kibana"</span>
        <span class="nb">kill</span> -9 <span class="sb">`</span>cat /var/run/kibana.pid<span class="sb">`</span>
    <span class="k">else</span>
        <span class="nb">echo</span> <span class="s2">"Programe don't run"</span>
    <span class="k">fi</span>
<span class="o">}</span>

<span class="c1"># 下面这些也用改</span>
<span class="c1">## Check to see if we are running as root first.</span>
<span class="c1">## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html</span>
<span class="k">if</span> <span class="o">[</span> <span class="s2">"</span><span class="k">$(</span>id -u<span class="k">)</span><span class="s2">"</span> !<span class="o">=</span> <span class="s2">"0"</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
    <span class="nb">echo</span> <span class="s2">"This script must be run as root"</span> <span class="m">1</span>&gt;<span class="p">&amp;</span><span class="m">2</span>
    <span class="nb">exit</span> <span class="m">1</span>
<span class="k">fi</span>

<span class="k">case</span> <span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span> in
    start<span class="o">)</span>
        start
        <span class="nb">exit</span> <span class="m">0</span>
    <span class="p">;;</span>
    stop<span class="o">)</span>
        stop
        <span class="nb">exit</span> <span class="m">0</span>
    <span class="p">;;</span>
    reload<span class="p">|</span>restart<span class="p">|</span>force-reload<span class="o">)</span>
        stop
        start
        <span class="nb">exit</span> <span class="m">0</span>
    <span class="p">;;</span>
    **<span class="o">)</span>
        <span class="nb">echo</span> <span class="s2">"Usage: </span><span class="nv">$0</span><span class="s2"> {start|stop|reload}"</span> <span class="m">1</span>&gt;<span class="p">&amp;</span><span class="m">2</span>
        <span class="nb">exit</span> <span class="m">1</span>
    <span class="p">;;</span>
<span class="k">esac</span>
</code></pre>
     </div>
     <h3 id="_4">
      添加服务
     </h3>
     <p>
      先赋予文件执行权限后使用命令将上面写的test添加到服务里面
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="n">chmod</span> <span class="o">+</span><span class="n">x</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">init</span><span class="p">.</span><span class="n">d</span><span class="o">/</span><span class="n">test</span>

<span class="o">#</span> <span class="n">Ubuntu</span><span class="p">,</span><span class="n">Kali下的操作</span>
<span class="o">#</span> <span class="k">update</span><span class="o">-</span><span class="n">rc</span><span class="p">.</span><span class="n">d</span> <span class="err">服务名</span> <span class="k">defaults</span><span class="err">，并且这样设置服务也就开机启动了</span>
<span class="k">update</span><span class="o">-</span><span class="n">rc</span><span class="p">.</span><span class="n">d</span> <span class="n">test</span> <span class="k">defaults</span>
<span class="o">#</span> <span class="err">删除</span>
<span class="o">#</span> <span class="k">update</span><span class="o">-</span><span class="n">rc</span><span class="p">.</span><span class="n">d</span> <span class="o">-</span><span class="n">f</span> <span class="err">服务名</span> <span class="n">remove</span>

<span class="o">#</span> <span class="n">CtenOS下的操作</span>
<span class="n">chkconfig</span> <span class="c1">--add agent_server</span>
<span class="n">chkconfig</span> <span class="n">agent_server</span> <span class="k">on</span>
</code></pre>
     </div>
     <h3 id="_5">
      启动、关闭、重启服务
     </h3>
     <p>
      接下来就可以使用service命令去启动它了
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="o">#</span> <span class="n">service</span> <span class="err">方式</span>
<span class="o">#</span> <span class="n">service</span> <span class="err">服务名</span> <span class="k">start</span>
<span class="o">#</span> <span class="n">service</span> <span class="err">服务名</span> <span class="n">stop</span>
<span class="o">#</span> <span class="n">service</span> <span class="err">服务名</span> <span class="k">start</span>
<span class="n">service</span> <span class="n">test</span> <span class="k">start</span>

<span class="o">#</span> <span class="n">systemctl</span>  <span class="err">方式</span>
<span class="n">systemctl</span> <span class="k">start</span> <span class="n">service</span>
<span class="n">systemctl</span> <span class="n">stop</span> <span class="n">service</span>
<span class="n">systemctl</span> <span class="k">restart</span> <span class="n">service</span>
<span class="n">systemctl</span> <span class="n">status</span> <span class="n">service</span>
</code></pre>
     </div>
     <h2 id="rclocal">
      编写rc.local文件
     </h2>
     <div class="codehilite">
      <pre><span></span><code><span class="err">chmod +x /etc/rc.d/rc.local</span>
<span class="err">vi /etc/rc.d/rc.local</span>
</code></pre>
     </div>
     <h2 id="_6">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://blog.csdn.net/xiongping_/article/details/50392908">
        update-rc.d的具体用法
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/xkjcf/article/details/78698232">
        在Ubuntu下添加自定义服务
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/37yan/p/6962563.html">
        shell 判断文件夹或文件是否存在
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/u013066244/article/details/69562364">
        linux后台启动进程并记录进程ID
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
